/**
 * 
 */
package com.alstudio.ui.module.props;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.local.Prop;
import com.alstudio.pojo.module.local.Props;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.ui.module.economy.MyWalletView;
import com.alstudio.view.viewpagerextend.ALViewpagerCreator;
import com.alstudio.view.viewpagerextend.FixedTabsView;
import com.alstudio.view.viewpagerextend.MyViewPager;
import com.loovee.imaohu.R;

/**
 * 道具商城
 * 
 * @author alonso lee
 * 
 */
public class PropsActivity extends TitleBarActivity implements
		OnPageChangeListener {

	private ArrayList<View> views;
	private MyViewPager pager;
	private FixedTabsView mFixedTabsView;
	private ALViewpagerCreator viewPagerCreator;
	private MyWalletView myWalletView;
	private ArrayList<String> titles = new ArrayList<String>();

	private ArrayList<Props> propList = new ArrayList<Props>();

	// 显示钱包
	private final int SHOW_TYPE_WALLET = 0;
	// 显示道具
	private final int SHOW_TYPE_PROPS = 1;
	// 当前显示模式
	private int curType = SHOW_TYPE_WALLET;

	private LinearLayout mPropsLayout;

	private ScrollView mScrollView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setCenterTextViewMessage(R.string.TxtUserProp);
		setTitleBarActivityContentView(R.layout.props_activity_layout);
		
		pager = (MyViewPager) findViewById(R.id.pager);
		mFixedTabsView = (FixedTabsView) findViewById(R.id.fixed_tabs);

		myWalletView = new MyWalletView(context);
		myWalletView.setUserGold(ALLocalEnv.getLocalEnv().getMyProfile()
				.getGold());

		mPropsLayout = new LinearLayout(getApplicationContext());
		mPropsLayout.setOrientation(LinearLayout.VERTICAL);

		mScrollView = new ScrollView(getApplicationContext());
		mScrollView.addView(mPropsLayout);

		// 加入title
		titles.add(getString(R.string.TxtGold));
		titles.add(getString(R.string.TxtProps));
		views = new ArrayList<View>();
		views.add(myWalletView);
		// views.add(propsListView);
		views.add(mScrollView);

		viewPagerCreator = new ALViewpagerCreator(ALLocalEnv.getLocalEnv()
				.getGlobalLayoutInflater(), titles, views);
		viewPagerCreator.setViewPager(pager);
		viewPagerCreator.setFixedTabsView(mFixedTabsView);
		viewPagerCreator.addOnPageChangeListener(this);
		viewPagerCreator.commit();

		hideRightButton(true);
		rightUseImageButton(true);
		setRightButtonImageSrc(0);

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		changeViewPage(arg0);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
	}

	@Override
	public void handleGetPropsListEvent(ALXmppEvent data) {
		// TODO Auto-generated method stub
		super.handleGetPropsListEvent(data);
		dismissLoadingProgress();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			propList.clear();
			@SuppressWarnings("unchecked")
			ArrayList<Props> tmp = (ArrayList<Props>) data.getData();

			propList.addAll(tmp);
			tmp.clear();
			tmp = null;
			// propsAdapter.notifyDataSetChanged();
			showPorps();
		}
	}

	private void changeViewPage(int index) {
		curType = index;
		if (curType == 1) {
			if (propList.size() == 0) {
				showLoadingProgress();
				ALUiXmppRequestCreator.postALUiXmppCheckPropsList();
			}
			// 当前显示的是道具，右边title要显示我的道具入口
			setRightButtonImageSrc(R.drawable.shop_title_icon_prop);
			// setRightButtonName(R.string.TxtMyProps);
			hideRightButton(false);
			setRightButtonClickListener(this);
		} else {
			// 当前显示的是钱包，右边title不要显示我的道具入口
//			setRightButtonImageSrc(R.drawable.shop_title_icon_prop);
			hideRightButton(true);
			setRightButtonClickListener(null);
		}
	}

	private void showPorps() {

		int line = 0;
		for (Props props : propList) {

			String title = props.getPropsName();
			final ArrayList<Prop> proplist = props.getPropitems();

			if (proplist == null || proplist.isEmpty())
				continue;

			int count = (proplist.size() - 1) / 4 + 1;
			for (int i = 0; i < count; ++i) {
				PropItem viewItem = new PropItem(this, PropItem.TYPE_MALL_PROPS);

				Prop p1, p2, p3, p4;
				int baseIndex = i * 4;
				if (proplist.size() > baseIndex) {
					p1 = proplist.get(baseIndex);
				} else {
					p1 = null;
				}

				if (proplist.size() > baseIndex + 1) {
					p2 = proplist.get(baseIndex + 1);
				} else {
					p2 = null;
				}

				if (proplist.size() > baseIndex + 2) {
					p3 = proplist.get(baseIndex + 2);
				} else {
					p3 = null;
				}

				if (proplist.size() > baseIndex + 3) {
					p4 = proplist.get(baseIndex + 3);
				} else {
					p4 = null;
				}

				viewItem.setProp(p1, p2, p3, p4);
				viewItem.setOnItemClickListener(onPropItemClickListener);

				if (i == 0) {
					viewItem.setTitle(title);

				} else {
					viewItem.setTitle(null);
				}

				mPropsLayout.addView(viewItem);
				++line;
			}

		}

		// for (int i = line; i < 5; ++i) {
		// PropItem viewItem = new PropItem(this, PropItem.TYPE_MALL_PROPS);
		// viewItem.setProp(null, null, null, null);
		// mPropsLayout.addView(viewItem);
		// }

	}

	/**
	 * 处理右键点击事件，跳转到我的道具
	 */
	protected void invokeRightBtnClick() {
		Intent i = new Intent(this, MyPropsActivity.class);
		startActivity(i);
	}

	private PropItem.OnItemClickListener onPropItemClickListener = new PropItem.OnItemClickListener() {

		@Override
		public void onItemClick(Prop p) {
			Intent i = new Intent(PropsActivity.this, PropsDetailActivity.class);

			i.putExtra("prop", p);
			startActivity(i);
		}
	};


}
